package com.yt.order.api.client;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.yt.common.constant.Result;
import com.yt.common.enums.CodeEnum;
import com.yt.order.models.bo.CreateOrderSaveBO;
import com.yt.order.models.vo.OrderInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
import com.yt.order.models.service.IOrderInfoService;
import com.yt.order.models.bo.OrderInfoBO;
import com.yt.order.models.dto.OrderInfoDTO;

import java.util.List;

/**
 * 订单信息控制器
 *
 * @author lvzeming
 * @Date 2025-08-22 21:02:52
 */

@RestController
@RequestMapping("/api/orderInfo")
@Slf4j
@Api(value = "OrderInfoApiController",tags = "订单信息Api接口")
public class OrderInfoApiController {

    @DubboReference
    private IOrderInfoService orderInfoService;

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<OrderInfoDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        OrderInfoDTO dto = orderInfoService.selectById(id);

        return new Result<>(dto);
    }

    @PostMapping("/createOrder")
    @ApiOperation(value = "创建订单",notes = "创建订单")
    public Result<OrderInfoVO> createOrder(@RequestBody CreateOrderSaveBO createOrderSaveBO){
        if(null == createOrderSaveBO){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        OrderInfoVO orderInfoVO = orderInfoService.createOrder(createOrderSaveBO);

        return new Result<>(orderInfoVO);
    }

    @ApiOperation(value = "订单信息列表",notes = "订单信息列表")
    @PostMapping("/queryPageList")
    public Result<Page<OrderInfoDTO>> queryPageList(OrderInfoBO bo){
        Wrapper<OrderInfoDTO> wrapper = new EntityWrapper<>();
        if (bo.getStartDate() != null){
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (bo.getEndDate() != null){
            wrapper.le("create_time", bo.getEndDate());

        }
        Page<OrderInfoDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = orderInfoService.selectPage(page, wrapper);

        return new Result<>(page);
    }
}
